<?php
namespace app\admin\controller;
use app\admin\validate\Admin as AdminValidate;

	class Admin extends Base
	{
		/**
		 * 管理员列表
		 */
		public function Admin(){
			if($this->request->isPost() && isset($_POST['lock'])){
				if(input('post.lock')==1){
					db('admin')->where('admin_id='.input('post.id'))->update(['is_lock'=>0]);
				}else{
					db('admin')->where('admin_id='.input('post.id'))->update(['is_lock'=>1]);
				};
				return $this->success('1');
			}
			$condition=[];
			if(input('username')){
				$condition[]=['user_name','like',"%".input('username')."%"];
			}
			if(input('start')!=''){
	    		$condition[]=[
	    			['add_time','>=',strtotime(input('start'))],
	    		];
	    	}
	    	if(input('end')!=''){
	    		$condition[]=[
	    			['add_time','<=',strtotime(input('end'))+86400],
	    		];
	    	}
			$info = model('Admin')
				->alias('a')
				->where($condition)
				->join('tp_admin_role r','a.role_id=r.role_id')
				->paginate([
					'list_rows'=>3,
					'query'=>input('param.'),
				]);
			$this->assign('info',$info);
			return $this->fetch();
		}

		/**
		 * 管理员添加
		 */
		public function add(){
			if($this->request->isPost()){
				$validate = new AdminValidate;
				$res = $validate
					->scene('add')
					->check(input('post.'));
				if(!$res){
					$result = $validate->getError();
					return $this->error($result);
				}
				$options = [
				    'cost' => 12,
				];
				$re = model('Admin')->insert([
					'user_name' =>  input("post.username"),
    				'password'  =>  password_hash(input('post.pwd'), PASSWORD_BCRYPT, $options),
    				'email'     =>  input("post.email"),
    				'phone'     =>  input("post.phone"),
    				'add_time'  =>  time(),
    				'role_id'   =>  input("post.roleid"),
    				'last_login'=>  0,
    				'last_ip'   =>  0,
    				'is_lock'   =>  0
				]);
				if($re>0){
					return $this->success('添加成功');
				}else{
					return $this->error('添加失败');
				}	
			}else{
				$rinfo = model("AdminRole")->select();
				$this->assign('rinfo',$rinfo);
				return $this->fetch();
			}
		}
		/**
		 * 管理员修改
		 */
		public function edit(){
			if($this->request->isPost()){
				$validate = new AdminValidate;
				if(input("post.pwd")==''){
					$res = $validate
					->scene('edit1')
					->check(input('post.'));
				}else{
					$res = $validate
					->scene('edit2')
					->check(input('post.'));
				}
				if(!$res){
					$result = $validate->getError();
					return $this->error($result);
				}
				if(input("post.pwd")==''){
					$re = model('Admin')->where("admin_id","=",input("post.admin_id"))->update([
						'user_name' =>  input("post.username"),
	    				'email'     =>  input("post.email"),
	    				'phone'     =>  input("post.phone"),
	    				'role_id'   =>  input("post.roleid"),
					]);
					//echo model('Admin')->getLastSql();
				}else{
					$re = model('Admin')->where("admin_id","=",input("post.admin_id"))->update([
						'user_name' =>  input("post.username"),
						'password'  =>  password_hash(input('post.pwd'), PASSWORD_BCRYPT, $options),
	    				'email'     =>  input("post.email"),
	    				'phone'     =>  input("post.phone"),
	    				'role_id'   =>  input("post.roleid"),
					]);
				}
				if($re==1){
					return $this->success('修改成功');
				}else{
					return $this->error('未进行任何修改');
				}
			}else{
				$rinfo = model("AdminRole")->select();
				$single = model("Admin")->where('admin_id',input('param.admin_id'))->select();
				$this->assign('single',$single[0]);
				$this->assign('rinfo',$rinfo);
				return $this->fetch();
			}
		}
		
		/**
		 * 管理员删除
		 */
		public function del(){
			$id=input('post.id');
			if(!empty(input('post.allid'))){
	    		$id=substr(input('post.allid'),0,-1);
	    	}
	    	$re = model('admin')->dele($id);
	    	if($re>0){
	    		return $this->success('删除成功');
	    	}else{
	    		return $this->error('删除失败');
	    	}
		}
	}